﻿$(document).ready(function () {
    $("#btnAddNew").click(function () {
        if ($(this).val() == "Add new") {
            $("#addnewbox").show();
            $(this).val("Hide");
        }
        else {
            $("#addnewbox").hide();
            $(this).val("Add new");
        }
    });
});

function EditInline(row, sender) {
    $(row).DoPendingChange();
    var ischange = $(row).attr("changed");
    if ((typeof ischange == "undefined" || ischange == "") && $(sender).text() != "Done") {
        $(row).InitialEditTableCell({ emptytext: '', inline: true });
        $(row).addClass("editing");
        $(sender).text("Done");
    } else if (ischange == "true") {
        SaveInline(row, sender);
        $(sender).text("Edit");
        $(row).DisableEditTableCell();
        $(row).removeClass("editing");
    } else {
        $(sender).text("Edit");
        $(row).DisableEditTableCell();
        $(row).removeClass("editing");
    }
}

function SaveInline(row, sender) {
    if (typeof $(row).attr("changed") != "undefined" && $(row).attr("changed") == "true") {
        var ID = $.trim($(row).attr("LocalCostID"));
        var Carr = $.trim($(row).children('[code="Carr"]').text());
        var Export20DC = $.trim($(row).children('[code="Export20DC"]').text());
        var Export40DC = $.trim($(row).children('[code="Export40DC"]').text());
        var Export20RF = $.trim($(row).children('[code="Export20RF"]').text());
        var Export40RF = $.trim($(row).children('[code="Export40RF"]').text());
        var Import20DC = $.trim($(row).children('[code="Import20DC"]').text());
        var Import40DC = $.trim($(row).children('[code="Import40DC"]').text());
        var Import20RF = $.trim($(row).children('[code="Import20RF"]').text());
        var Import40RF = $.trim($(row).children('[code="Import40RF"]').text());
        var Seal = $.trim($(row).children('[code="Seal"]').text());
        var BL = $.trim($(row).children('[code="BL"]').text());
        var Telex = $.trim($(row).children('[code="Telex"]').text());
        var ENS = $.trim($(row).children('[code="ENS"]').text());
        var LatePayMentFEE = $.trim($(row).children('[code="LatePayMentFEE"]').text());
        var CIC = $.trim($(row).children('[code="CIC"]').text());
        var EBS = $.trim($(row).children('[code="EBS"]').text());
        var Remark = $.trim($(row).children('[code="Remark"]').text());
        var VSCont = ""; //$.trim($(row).children('[code="VSCont"]').text());

        $.ajax({
            type: 'POST',
            url: "Handlers/EditInline.ashx",
            data: {
                act: "localcost",
                Carr: Carr,
                ID: ID,
                Export20DC: Export20DC,
                Export40DC: Export40DC,
                Export20RF: Export20RF,
                Export40RF: Export40RF,
                Import20DC: Import20DC,
                Import40DC: Import40DC,
                Import20RF: Import20RF,
                Import40RF: Import40RF,
                Seal: Seal,
                BL: BL,
                Telex: Telex,
                ENS: ENS,
                LatePayMentFEE: LatePayMentFEE,
                CIC: CIC,
                EBS: EBS,
                Remark: Remark,
                VSCont: VSCont
            },
            success: function (data) {
                if (data == null) {
                    alert("Cập nhật không thành công!");
                } else if (data.Message == "sucessful") {
                    //$(row).children('td[code="LastUpdate"]').text(data.Date);
                } else {
                    alert(data.Message);
                }

                $(row).removeAttr("changed");
                $(row).DisableEditTableCell();
                $(sender).text("Edit");
            },
            dataType: "json"
        });
    } else {
        $(sender).text("Edit");
        $(row).DisableEditTableCell();
    }
}

